The basic libraries needed for processing the data and making some simple plots
library(httr)
library(maptools)
## Loading required package: sp
## Checking rgeos availability: FALSE
## Note: when rgeos is not available, polygon geometry computations in maptools depend on gpclib,
## which has a restricted licence. It is disabled by default;
## to enable gpclib, type gpclibPermit()
library(maps)
library(mapdata)
library(rjson)
library(stringr)
library(RCurl)
## Loading required package: bitops
library(ggplot2)
library(plyr)
library(ggmap)
library(knitr)
opts_chunk$set(results='hide',
warning=F,fig.align='center',echo=T,
fig.height=12,message=F)
Load the data from the Strava site
source("Rtrava.R")
# For the first time to create the token run the following
# source("login.R") # defines everything for strava_oath function
# stoken<-config(token=strava_oauth(strava.appname,strava.clientid,strava.secret))
# save.image("token.RData")
load("token.RData");
# reload the important functions
source("Rtrava.R")
usage_left <- as.integer(c(600, 30000))
Read my activities
my.strava.id<-"845403"
my.acts<-get_activity_list(stoken,id=my.strava.id)
Show a single polyline
first.polyline<-DecodeLineR(my.acts[[1]]$map$summary_polyline)
ggplot(first.polyline,aes(y=lat,x=lng))+
geom_jitter()+
geom_path()+
labs(y="Latitude",x="Longitude")+
theme_bw(20)
Parse the rest of it into a data-frame so it can be manipulated and plotted easily
try.or.na<-function(block) {
out.val<-tryCatch(block,error=function(e) {NA})
if(is.null(out.val)) NA
else out.val
}
sum.fun<-function(cur.act) {
data.frame(id=try.or.na({cur.act$id}),
date=try.or.na({cur.act$start_date_local}),
type=try.or.na({cur.act$type}),
workout_type=try.or.na({cur.act$workout_type}),
avg_speed=try.or.na({cur.act$average_speed}),
avg_hr=try.or.na({cur.act$average_heartrate}),
dist=try.or.na({cur.act$distance}),
elevation=try.or.na({cur.act$total_elevation_gain}),
moving_time=try.or.na({cur.act$moving_time})
)
}
summary.data<-ldply(my.acts,sum.fun)
pl.data<-ldply(my.acts,function(cur.act) {
sum.data<-sum.fun(cur.act)
# append position information
tryCatch(
{
map.df<-DecodeLineR(cur.act$map$summary_polyline)
cbind(sum.data,map.df)
},
error = function(e) {
cbind(sum.data,lat=NA,lng=NA)
})
})
ggplot(pl.data,aes(x=lng,y=lat))+
stat_binhex(color=NA,bins=100)+
geom_density2d()+
coord_equal()+
labs(y="Latitude",x="Longitude")+
theme_bw(20)
Now show a basic density plot on top of some map information for around Switzerland
villigenMap<-get_map(location=c(lon=8.22190,lat=47.53733),color="bw")
ggmap(villigenMap)+
stat_binhex(data=pl.data,aes(x=lng,y=lat),alpha=0.5,bins=200)+
scale_fill_gradientn(colours=rainbow(6),trans = "log")+
labs(y="Latitude",x="Longitude")+
theme_bw(20)
Now of the US
usaMap<-get_map(location=c(lon=-97.22190,lat=42),zoom=4,color="bw")
ggmap(usaMap)+
stat_binhex(data=pl.data,aes(x=lng,y=lat),alpha=0.5,bins=100)+
scale_fill_gradientn(colours=rainbow(6),trans = "log")+
labs(y="Latitude",x="Longitude")+
theme_bw(20)
Now of Europe
euMap<-get_map(location=c(lon=12.22190,lat=45),zoom=4,color="bw")
ggmap(euMap)+
stat_binhex(data=pl.data,aes(x=lng,y=lat),alpha=0.5,bins=50)+
scale_fill_gradientn(colours=rainbow(6),trans = "log")+
labs(y="Latitude",x="Longitude")+
theme_bw(20)
good.sport.data<-subset(summary.data,!is.na(avg_hr) & avg_speed<80)
good.sport.data$year<-as.numeric(substr(as.character(good.sport.data$date),0,4))
good.sport.data$kmph<-with(good.sport.data,dist/1000/(moving_time/3600))
good.sport.data$minperkm<-with(good.sport.data,(moving_time/60)/(dist/1000))
ggplot(good.sport.data,aes(x=moving_time/60,y=dist/1000))+
geom_point(aes(color=avg_hr))+
geom_smooth()+
scale_color_gradientn(colours=rainbow(6))+
labs(x="Time (min)",y="Distance (km)")+
facet_wrap(~type,scales="free")+
theme_bw(10)
ggplot(good.sport.data,aes(x=avg_hr,y=minperkm,color=type))+
geom_point()+
geom_density2d()+
#scale_color_gradientn(colours=rainbow(6))+
labs(y="Pace (min/km)",x="Heart Rate")+
#facet_wrap(~type,scales="free")+
ylim(0,45)+
theme_bw(20)
ggplot(good.sport.data,aes(x=moving_time/60,y=avg_hr))+
geom_point(aes(color=type))+
#scale_color_gradientn(colours=rainbow(6))+
labs(x="Time",y="Heart Rate")+
scale_x_sqrt()+
facet_wrap(~type,scales="free")+
theme_bw(10)
run.data<-subset(good.sport.data,!is.na(avg_hr) & type=="Run" & minperkm>3 & minperkm<30)
ggplot(run.data,aes(x=moving_time/60,y=avg_hr))+
geom_point(aes(color=minperkm))+
scale_color_gradientn(colours=rainbow(6))+
labs(x="Time (minutes)",y="Heart Rate",color="Pace\nMin/km")+
scale_x_sqrt()+
theme_bw(20)
ggplot(run.data,aes(x=minperkm,y=avg_hr))+
geom_point(aes(color=moving_time/60))+
scale_color_gradientn(colours=rainbow(6))+
labs(x="Pace (Min/km)",y="Heart Rate",color="Time\n(min)")+
theme_bw(20)
ggplot(run.data,aes(x=minperkm,y=avg_hr))+
geom_point(aes(color=dist/1000))+
scale_color_gradientn(colours=rainbow(6))+
labs(x="Speed",y="Heart Rate",color="Distance\nkm")+
facet_wrap(~year)+
ylim(100,180)+
theme_bw(20)
ggplot(run.data,aes(x=minperkm,y=avg_hr,color=as.factor(year)))+
geom_density2d(aes(weight=moving_time))+
labs(x="Pace (min/km)",y="Heart Rate")+
ylim(100,180)+
#xlim(1,20)+
facet_wrap(~year)+
labs(color="Year")+
theme_bw(20)
ggplot(run.data,aes(x=as.factor(year),y=minperkm,fill=as.factor(year)))+
geom_violin()+
labs(y="Pace (min/km)",x="Year")+
labs(fill="Year")+
theme_bw(20)
cycling.data<-subset(good.sport.data,!is.na(avg_hr) & type=="Ride" & kmph>5 & kmph<60)
ggplot(cycling.data,aes(x=as.factor(year),y=kmph,fill=as.factor(year)))+
geom_violin()+
labs(y="Speed",x="Year")+
#ylim(1,5)+
labs(fill="Year")+
theme_bw(20)
ggplot(cycling.data,aes(x=kmph,y=avg_hr))+
geom_point(aes(color=elevation))+
geom_density2d(alpha=0.5)+
labs(x="Pace (min/km)",y="Heart Rate")+
scale_color_gradientn(colours=rainbow(6))+
ylim(100,180)+
#xlim(1,20)+
facet_wrap(~year)+
theme_bw(20)